package wang.ddcat.reptilian_plus.web;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import wang.ddcat.reptilian_plus.constant.UserAgentConstant;
import wang.ddcat.reptilian_plus.dto.InformationDto;
import wang.ddcat.reptilian_plus.link.Links;
import wang.ddcat.reptilian_plus.page.Page;
import wang.ddcat.reptilian_plus.page.PageParserTool;
import wang.ddcat.reptilian_plus.util.JsonResult;
import wang.ddcat.reptilian_plus.util.WebCrawler;

import java.util.ArrayList;
import java.util.List;

@RestController
public class MainController {

    /**
     * @param lang 爬取的语言  java/php/python/c++/html/。。。。。
     * @param threadCount 需要开启的线程个数
     * @param size 爬取多少页
     * @return
     */
    @GetMapping("start")
    public Object start(@RequestParam(value = "lang",required = false,defaultValue = "Java")String lang,
                        @RequestParam(value = "threadCount",required = false,defaultValue = "10")Integer threadCount,
                        @RequestParam(value = "size",required = false,defaultValue = "10")Integer size){
        List<InformationDto> informations = new ArrayList<>();

        String[] urls = new String[size];
        for(int i=1;i<=size;i++){
            String url = "https://gitee.com/explore/starred?lang="+lang+"&page="+i;
            urls[i-1] = url;
        }

        List<Page> pages = new WebCrawler()
                //设置用户浏览器请求标识
                .user_agent(UserAgentConstant.PC_Chrome)
                //用户链接超时
                .timeout(6000)
                //用户缓存
                .use_caches(false)
                //爬虫开始线程
                .threadCount(threadCount)
                //是否清除上次爬虫任务已经爬取所存在链接(避免重复爬取)
                .removeVisitedUrlSet(true)
                //开启爬虫。。。
                .start(urls);

        // 解析网页 规则
        for(Page page : pages){
            Document doc = page.getDoc();
            Elements items = doc.select("div.projects-container div#git-discover-list").select("div.item");
            for(Element item : items){
                try {
                    InformationDto information = new InformationDto();
                    Element img_ele = item.select("a.project-creator-link").first().select("img").first();
                    String logo_src = PageParserTool.getAttr(img_ele,"src");
                    information.setLogo(logo_src);//作者头像
                    Element content_ele = item.select("div.content").first();//内容
                    Element title_ele = content_ele.select("div.project-title").first();
                    String title = title_ele.select("h3 a").text();
                    information.setTitle(title);//标题
                    Elements watch_star_fork_eles = title_ele.select("div.watch-star-fork a");
                    try{
                        String follow_num = watch_star_fork_eles.get(0).select("span").text();
                        information.setFollow_num(follow_num);//关注数
                        String collection_num = watch_star_fork_eles.get(1).select("span").text();
                        information.setCollection_num(collection_num);//收藏数
                        String fork_num = watch_star_fork_eles.get(2).select("span").text();
                        information.setFork_num(fork_num);//fork数
                    }catch (Exception e){}
                    String project_desc = content_ele.select("div.project-desc").text();
                    information.setProject_desc(project_desc);//项目简介
                    String project_meta = content_ele.select("div.project-meta a").text();
                    information.setProject_meta(project_meta);//项目标签
                    String timeago = content_ele.select("div.project-meta div.pull-right span.timeago").text();
                    information.setTimeago(timeago);//项目时间

                    informations.add(information);
                }catch (Exception e){
                    continue;
                }
            }
        }
        return JsonResult.success(informations);
    }

}
